package com.qiaoyanqing.myqiubai;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder>{
    private Context context;
    private List<ItemEntity> list;
    public static final String IMAGE_URL = "http://pic.qiushibaike.com/system/pictures/%s/%s/medium/%s";
    public static final String ICON_URL = "http://pic.qiushibaike.com/system/avtnew/%d/%d/thumb/%s";
    public static final String TAG = ItemAdapter.class.getSimpleName();
    public ItemAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ItemEntity item = list.get(position);
        if (item.getUser() != null) {
            holder.userInfo.setVisibility(View.VISIBLE);
            holder.userName.setText(item.getUser().getLogin());
            UserEntity user = item.getUser();
            String url = String.format(Locale.getDefault(), ICON_URL, user.getUid() / 10000, user.getUid(), user.getIcon());
            Picasso.with(context).load(url).transform(new CircleTransFoem()).into(holder.userIcon);
        } else {
            holder.userInfo.setVisibility(View.GONE);
        }
        if (item.getImage() != null) {
            holder.image.setVisibility(View.VISIBLE);
            String image = item.getImage();
            Pattern pattern = Pattern.compile("(\\d+)\\d{4}");
            Matcher matcher = pattern.matcher(image);
            if (matcher.find()) {
                String url = String.format(Locale.getDefault(), IMAGE_URL, matcher.group(1), matcher.group(), image);
                Picasso.with(context).load(url).into(holder.image);
            }
        } else if (item.getPic_url() != null) {
            holder.image.setVisibility(View.VISIBLE);
            Picasso.with(context).load(item.getPic_url()).into(holder.image);
        } else {
            holder.image.setVisibility(View.GONE);
        }
        holder.content.setText(item.getContent());

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void addAll(Collection<? extends ItemEntity> collection) {
        int size = list.size();
        list.addAll(collection);
        notifyItemRangeInserted(size, collection.size());
    }

//    public void addAll(Collection<?extends  ItemEntity>collection){
//          int size=list.size();
//          list.addAll(collection);
//          notifyItemRangeInserted(size,collection.size());
//    }



    public static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView userIcon;
        private TextView userName;
        private LinearLayout userInfo;
        private TextView content;
        private ImageView image;

        public ViewHolder(View itemView) {
            super(itemView);
            userIcon = ((ImageView) itemView.findViewById(R.id.user_icon));
            userName = ((TextView) itemView.findViewById(R.id.user_name));
            userInfo = ((LinearLayout) itemView.findViewById(R.id.user_info));
            content = ((TextView) itemView.findViewById(R.id.item_content));
            image = ((ImageView) itemView.findViewById(R.id.item_image));
        }
    }
}
